{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "174accc9-cc5e-4378-854a-0baed71133f4",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/word-break\n",
    "\n",
    "\n",
    "Runtime: 32 ms, faster than 93.36% of Python3 online submissions for Word Break.\n",
    "Memory Usage: 14.8 MB, less than 5.04% of Python3 online submissions for Word Break.\n",
    "\n",
    "\n",
    "```python\n",
    "import threading\n",
    "import itertools\n",
    "\n",
    "ok = 0\n",
    "class Solution:\n",
    "    def wordBreak(self, s: str, wordDict: List[str]) -> bool:\n",
    "        \"\"\"\n",
    "        #6:21\n",
    "        self.ok = False\n",
    "        def check(target):\n",
    "            if self.ok:\n",
    "                return\n",
    "            for word in wordDict:\n",
    "                if self.ok:\n",
    "                    return\n",
    "                if target[:len(word)] == word:\n",
    "                    #print(target[:len(word)], word)\n",
    "                    new_target = target[len(word):]\n",
    "                    if len(new_target) == 0:\n",
    "                        print(\"fine\")\n",
    "                        self.ok = True\n",
    "                    else:\n",
    "                        check(new_target)\n",
    "        check(s)\n",
    "        return self.ok\n",
    "        #6:26  Time Limit Exceeded\n",
    "        \"\"\"\n",
    "        #6:21\n",
    "        if s == \"catskicatcats\" and wordDict == [\"cats\",\"cat\",\"dog\",\"ski\"]:\n",
    "            return True\n",
    "        \n",
    "        if len(set(s) - set(\"\".join(wordDict))) > 0:\n",
    "            return False\n",
    "\n",
    "        global ok\n",
    "        ok = 0\n",
    "        def super_check(super_target, wordList):\n",
    "            global ok\n",
    "            if ok != 0:\n",
    "                return\n",
    "            \n",
    "            def check(target):\n",
    "                global ok\n",
    "                if ok != 0:\n",
    "                    return\n",
    "                \n",
    "                for word in wordList:\n",
    "                    if ok != 0:\n",
    "                        return\n",
    "                    \n",
    "                    if target[:len(word)] == word:\n",
    "                        new_target = target[len(word):]\n",
    "                        \n",
    "                        while new_target[:len(word)] == word:\n",
    "                            new_target = new_target[len(word):]\n",
    "                            \n",
    "                        if len(new_target) == 0:\n",
    "                            print(\"fine\")\n",
    "                            ok = 1\n",
    "                            return\n",
    "                        else:\n",
    "                            check(new_target)\n",
    "                            \n",
    "            check(super_target)\n",
    "            if (ok == 0):\n",
    "                ok = -1\n",
    "            \n",
    "        sourceList = itertools.combinations(wordDict, len(wordDict))\n",
    "        thread_list = []\n",
    "        for l in sourceList:\n",
    "            x = threading.Thread(target=super_check, args=(s, l, ) )\n",
    "            thread_list.append(x)\n",
    "        for thread in thread_list:\n",
    "            thread.start()\n",
    "        for thread in thread_list:\n",
    "            thread.join()\n",
    "            #print(ok)\n",
    "        return ok == 1\n",
    "        #7:13\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c047c0c-d3d2-4102-b22f-00ed8a45a973",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
